// 导入
require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.less')
require('../font/iconfont.css')
require('../css/train.less')
// 导入axios
require('axios')

// 导入weui
require('weui')
const {
    default: axios
} = require('axios')
const weui = require('weui.js')

document.addEventListener('DOMContentLoaded', function () {

    //接收课程视频数据
    const fragments = JSON.parse(sessionStorage.fragments)
    // console.log(fragments);
    // 总训练个数
    let maxNum = fragments.length
    // 获取元素
    // 视频
    const video = document.querySelector('video')
    // 当前训练视频个数
    const nowNum = document.querySelector('#nowNum')
    // 总个数
    const totalNum = document.querySelector('#totalNum')
    // 介绍
    const introduce = document.querySelectorAll('.introduce')
    // 上一节
    const backBtn = document.querySelector('#backBtn')
    // 暂停
    const stopBtn = document.querySelector('#stopBtn')
    // 下一节
    const nextBtn = document.querySelector('#nextBtn')
    // 图片
    const img = document.querySelector('#img')
    // 索引
    let index = 0
    // 模态框
    const videoMask = document.querySelector('#videoMask')
    // 继续按钮
    const goOnBtn = document.querySelector('#goOnBtn')
    // 结束按钮
    const endBtn = document.querySelector('#endBtn')
    // 进度条
    const pro = document.querySelector('#pro')
    // 开始训练时间&继续训练时间
    let time1 = new Date()
    // 暂停时的时间
    let time2 = null
    // 总时间
    let time3 = null
    // 封装函数：渲染加载的数据
    randerVideo()

    function randerVideo() {
        video.src = 'http://139.9.177.51:8099' + fragments[index].videoUrl
        img.src = 'http://139.9.177.51:8099' + fragments[index].imgUrl
        introduce.forEach(function (value) {
            value.textContent = fragments[index].title
        })
        nowNum.textContent = index + 1
        totalNum.textContent = maxNum
    }
    // 上一节
    backBtn.addEventListener('click', function () {
        index--
        if (index < 0) {
            index = maxNum - 1
        }
        randerVideo()
    })
    // 下一节
    nextBtn.addEventListener('click', function () {
        index++
        if (index === maxNum) {
            index = 0
        }
        randerVideo()
    })
    // 暂停
    stopBtn.addEventListener('click', function () {
        videoMask.style.display = 'block'
        video.pause()
        time2 = new Date()
        time3 += (time2 - time1) / 1000
    })
    // 继续
    goOnBtn.addEventListener('click', function () {
        videoMask.style.display = 'none'
        video.play()
        time1 = new Date()
    })
    // 结束
    endBtn.addEventListener('click', function () {

        // console.log(time3.toFixed(2)); // 获取训练时间
        // 卡路里
        let calorie = (time3.toFixed(2) * 0.36).toFixed(2)
        const params = {
            id: sessionStorage.cid,
            userId: localStorage.userId,
            takeTime: time3.toFixed(2),
            calorie: calorie
        }
        console.log(params);
        // 发送请求提交训练数据
        axios.post('http://139.9.177.51:8099/sports/saveTrain', params).then(function (res) {
            console.log(res.data);
            if (res.data.status === 0) {
                weui.toast(`结束训练共消耗${calorie}卡路里`, {
                    callback() {
                        location.href = './motion-curriculum.html'
                    }
                })
            } else {
                weui.toast('您辛苦了')
            }
        })
    })
    // 视频进度条
    video.addEventListener('timeupdate', function () {
        // 当前进度
        let progress = video.currentTime / video.duration * 100
        // console.log(progress);
        pro.style.width = progress + '%'
    })
    // 视频结束时跳转到下个视频
    video.addEventListener('ended', function () {

        weui.toast('即将播放下一个视频', )
        setTimeout(function () {
            nextBtn.click();
        }, 3500)
    })


})